package com.af.robot.scanner;


import com.af.robot.entity.Structure;
import com.af.robot.exception.CustomerException;
import com.af.robot.result.ResultEnum;
import com.af.robot.util.Unit;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Component;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Component
public class Catalogue {

    public JSONObject getList(String Absolute,String replace){
        return getAll(Absolute,null,null,replace);
    }

    public JSONObject getType(String Absolute,String type,String replace){
        return getAll(Absolute,type,null,replace);
    }

    public JSONObject getLikeName(String Absolute,String name,String replace){
        return getAll(Absolute,null,name,replace);
    }

    public JSONObject getLikeNameType(String Absolute,String type,String name,String replace){
        return getAll(Absolute,type,name,replace);
    }

    private JSONObject getAll(String Absolute,String type,String name,String replace){
        File dir = new File(Absolute);
        if( !dir.exists() ) throw new CustomerException(ResultEnum.PATH_INVALID);
        if( !dir.isAbsolute() ) throw new CustomerException(ResultEnum.PATH_NO_ABSOLUTE);
        if( !dir.isDirectory() ) throw new CustomerException(ResultEnum.PATH_NO_FOLDER);
        File[] fileList = dir.listFiles();
        //目录列表
        List<File> files = Arrays.asList(fileList == null ? new File[0] : fileList);

        //筛选并排序生成列表
        List<JSONObject> collect = files.stream().filter(file -> {
            if (type != null) {
                if (file.isDirectory())
                    return false;
                String fileType = file.getName().substring(file.getName().lastIndexOf(".")+1);
                return type.equals(fileType);
            } else return true;
        }).filter(file -> {
            if (name != null) {
                return file.getName().contains(name);
            } else return true;
        }).sorted((o1, o2) -> {
            if (o1.isDirectory() && o2.isFile())
                return -1;
            if (o1.isFile() && o2.isDirectory())
                return 1;
            return o1.getName().compareTo(o2.getName());
        }).map(file -> {
            Structure structure = new Structure();
                structure.setName(file.getName())
                        .setAbsolute(file.getAbsolutePath().replace(replace,""))
                        .setType(file.isDirectory())
                        .setDate(Unit.millis2TimeString(file.lastModified()))
                        .setSize(Unit.byte2SizeString(file.length()));
            return structure.getJson();
        }).collect(Collectors.toList());

        JSONObject result = ResultEnum.OK.getResult();
        result.put("data",collect);
        return result;
    }


    public BufferedInputStream getFileStream(String Absolute){
        File file = new File(Absolute);
        if ( !file.exists()){
            return null;
        }
        try {
            return new BufferedInputStream(new FileInputStream(file));
        } catch (FileNotFoundException e) {
            return null;
        }
    }

}
